package io.leo.leoui.utils;

import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class StatubarUtils {

    public static final int ANDROID_6_OR_HIGHER=0,MIUIV6_OR_HIGHER=1,FLYME4_OR_HIGHER=2,UNKNOW=4;

    /**
     * 状态栏白底黑字
     *
     * 兼容Android6.0+、MIUIV6+、Flyme4.0+
     * @return 返回推测的系统版本 0 Android6+ 1 MIUIv6+ 2 Flyme4+ 3其他
     */
    public static int lightStatubar(Window window){
        if (MIUISetStatusBarLightMode(window))return MIUIV6_OR_HIGHER;
        else if (FlymeSetStatusBarLightMode(window))return FLYME4_OR_HIGHER;
        else if (AndroidMStatusBarLightMode(window))return ANDROID_6_OR_HIGHER;
        else return UNKNOW;
    }

    /**
     * Android M以上的白底黑字状态栏
     * @param window
     * @return
     */
    public static boolean AndroidMStatusBarLightMode(Window window){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            return true;
        }
        return false;

    }

    /**
     * Flyme4以上的白底黑字状态栏
     * @param window
     * @return
     */
    public static boolean FlymeSetStatusBarLightMode(Window window) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                value |= bit;
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * MIUI6以上的白底黑字状态栏
     * @param window
     * @return
     */
    public static boolean MIUISetStatusBarLightMode(Window window) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }else {
                    int darkModeFlag = 0;
                    Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                    Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                    darkModeFlag = field.getInt(layoutParams);
                    Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                }
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

}
